package com.haipumi.listeners;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.haipumi.biz.TaskBiz;
import com.haipumi.biz.impl.TaskBizImpl;
import com.haipumi.utils.CommonData;

public class RankListener implements ServletContextListener {
	private static boolean ThreadFlag=false;//判断
	private TaskBiz tb=new TaskBizImpl();
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		//当销毁时,关闭线程
		ThreadFlag=false;
	}

	@Override
	public void contextInitialized(final ServletContextEvent arg0) {
		ThreadFlag=true;
		//开启线程,每一个小时查一次数据库,将信息存到application中
		new Thread(new Runnable(){	//创建线程
			public void run(){
				while(ThreadFlag){
					Map<String,String> map=tb.findRank();
					ServletContext application=arg0.getServletContext();
					application.setAttribute(CommonData.APPLICATIONRANK, map);
					try {
						Thread.sleep(1000*60*60);	//每个小时执行一次
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

}
